#!/usr/bin/env python
from __future__ import print_function
from __future__ import division

from os.path import join
from pprint import pprint

import Options

import OpenGL
if not Options.AreDebugging:
    OpenGL.ERROR_CHECKING = False
if not Options.AreDebugLogging:
    OpenGL.ERROR_LOGGING = False
from OpenGL.GL import *
from OpenGL.GLU import *

import Map
from Engine import Window
#, Vector, Color, glStrings, Events
#from Engine.GLWrappers import *


class GameWindow(Window.Window):
    def InitGL(self):
        # Run-once setup stuffs.
        glClearDepth(1.0)
        #glDepthFunc(GL_LEQUAL)
        glEnable(GL_NORMALIZE)
        #glShadeModel(Options.ShadeModel)
        glFrontFace(GL_CW)
        glEnable(GL_CULL_FACE)
        #glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
        #glEnable(GL_BLEND)
        #glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        # Set the background color
        glClearColor(0.4, 0.4, 0.4, 1.0)
        glEnable(GL_LIGHTING)
        #glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (0.5, 0.5, 0.5, 1.0))
        glLight(GL_LIGHT0, GL_AMBIENT, (1.0, 1.0, 1.0, 1.0))
        glLight(GL_LIGHT0, GL_POSITION, (0.0, 2.0, 2.0))
        glEnable(GL_LIGHT0)
        glEnable(GL_COLOR_MATERIAL)
        glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE)

        glShadeModel(GL_SMOOTH)
        self.OnSize(*resolution)

        self.worldMap = Map.Map(3, 3, 3)
        self.worldMap.load("1")
        ##camera.center(worldMap.center)
        self.worldMap.build()

    def OnDraw(self):
        # draw six faces of a cube
        self.worldMap.draw()
        glBegin(GL_QUADS)

        glColor(0.0, 0.0, 0.0, 1.0)
        glVertex3f( 0.5, 0.5, 0.5)
        glVertex3f( 0.5,-0.5, 0.5)
        glVertex3f(-0.5,-0.5, 0.5)
        glVertex3f(-0.5, 0.5, 0.5)

        glColor(0.0, 0.0, 1.0, 1.0)
        glVertex3f(-0.5,-0.5,-0.5)
        glVertex3f( 0.5,-0.5,-0.5)
        glVertex3f( 0.5, 0.5,-0.5)
        glVertex3f(-0.5, 0.5,-0.5)

        glColor(0.0, 1.0, 0.0, 1.0)
        glVertex3f( 0.5, 0.5, 0.5)
        glVertex3f(-0.5, 0.5, 0.5)
        glVertex3f(-0.5, 0.5,-0.5)
        glVertex3f( 0.5, 0.5,-0.5)

        glColor(0.0, 1.0, 1.0, 1.0)
        glVertex3f(-0.5,-0.5,-0.5)
        glVertex3f(-0.5,-0.5, 0.5)
        glVertex3f( 0.5,-0.5, 0.5)
        glVertex3f( 0.5,-0.5,-0.5)

        glColor(1.0, 0.0, 0.0, 1.0)
        glVertex3f( 0.5, 0.5, 0.5)
        glVertex3f( 0.5, 0.5,-0.5)
        glVertex3f( 0.5,-0.5,-0.5)
        glVertex3f( 0.5,-0.5, 0.5)

        glColor(1.0, 0.0, 1.0, 1.0)
        glVertex3f(-0.5,-0.5,-0.5)
        glVertex3f(-0.5, 0.5,-0.5)
        glVertex3f(-0.5, 0.5, 0.5)
        glVertex3f(-0.5,-0.5, 0.5)

        glEnd()


if __name__ is '__main__':
    resolution = (800, 600)
    cam_position = (0.0, 0.0, 5.0)
    cam_focus = (0.0, 0.0, 0.0)
    cam_up = (0.0, 1.0, 0.0)
    perspective = Window.Perspective(65.0, 0.0, 100.0, resolution)
    camera = Window.Camera(cam_position, cam_focus, cam_up)
    myWindow = GameWindow(perspective, camera, resolution)
    #events = Events.EventSystem()
    state = "Introduction"
    # Psyco if available
    if Options.AreJITing:
        try:
            import psyco
            if Options.AreDebugLogging:
                psyco.log()
            psyco.profile(1.0 - Options.JITProfilingIntensity)
            psyco.full()

        except ImportError:
            pass

    while True:
        myWindow.Loop()